package com.globalwave.filemanage.web;


import com.globalwave.base.BaseAction;
import com.globalwave.common.ArrayOrderList;
import com.globalwave.common.ArrayPageList;
import com.globalwave.filemanage.entity.Document;
import com.globalwave.filemanage.entity.DocumentCriterion;
import com.globalwave.filemanage.entity.SearchCriterion;
import com.globalwave.filemanage.service.DocumentService;
import com.globalwave.system.web.annotations.Pid;
import com.opensymphony.xwork2.Preparable;

public class DocumentAction extends BaseAction implements Preparable {
   
    private static final long serialVersionUID = 1L;

    private DocumentService documentService = null ;
    private DocumentCriterion criterion = null ;
    private SearchCriterion options = null ;
    private Document document ;
    
    public String execute() throws Exception {
        return this.list();    
        
    }
    
    public String list() throws Exception {        
        ArrayOrderList order = new ArrayOrderList() ;
        order.addAsc("document_id") ;
        ArrayPageList<Document> pageList = documentService.query(criterion,pageIndex,pageSize,order) ;

        renderXML(pageList.asXML()) ;
        
        return null ;    
        
    }
    
    @Pid(value=132)
    public String create()  throws Exception {        
                
        final Object newDocument = documentService.create(document) ;
        
        renderXML(this.getSuccessCreateMessage(newDocument).asXML()) ;
        
        return null;    
        
    }
    
    @Pid(value=133)
    public String update()  throws Exception {     
        
        documentService.update(document) ;

        renderXML(this.getSuccessUpdateMessage(document).asXML()) ;
        
        return null;    
        
    }
    
    @Pid(value=134)
    public String delete()  throws Exception {
        
        if (ids == null) {
            documentService.delete(document) ;
        } else {
            documentService.deleteAll(ids) ;
        }

        renderXML(this.getSuccessDeleteMessage().asXML()) ;
        
        return null;    
        
    }
    
    public String search() throws Exception {      

        ArrayPageList<Document> pageList = documentService.search(options,pageIndex,pageSize) ;

        renderXML(pageList.asXML()) ;
        
        return null ;    
    }
    
    public String listPhotos() throws Exception {        
        ArrayOrderList order = new ArrayOrderList() ;
        order.addAsc("document_id") ;
        
        ArrayPageList<Document> pageList = documentService.listPhoto(criterion,pageIndex,pageSize,order) ;

        renderXML(pageList.asXML()) ;
        
        return null ;    
        
    }
    
    public void prepare() throws Exception {
        // TODO Auto-generated method stub

    }
        
    // ------------------------------------------------------------------------
    
    // ioc
    public void setDocumentService(DocumentService documentService) {
        this.documentService = documentService;
    }

    // entity...
    public Document getDocument() {
        return document;
    }

    public void setDocument(Document document) {
        this.document = document;
    }

	public DocumentCriterion getCriterion() {
		return criterion;
	}

	public void setCriterion(DocumentCriterion criterion) {
		this.criterion = criterion;
	}
	public void setOptions(SearchCriterion options) {
		this.options = options;
	}
	public SearchCriterion getOptions() {
		return options;
	}
}
